<?php

namespace Service\HelperService;

class Common {
    public static $pre_image_url = "https://indoorbuy.oss-cn-hangzhou.aliyuncs.com/";

    //默认分级
    public static $pic_level = array(
        'big'          => '?x-oss-process=image/crop,x_0,y_0,w_1000,h_1000,g_se',//大图：以宽度和高度裁剪，裁剪最长为1000pc
        'mid'          => '?x-oss-process=image/crop,x_0,y_0,w_500,h_500,g_se',//中图：以宽度和高度裁剪，裁剪最长为500pc
        'small'        => '?x-oss-process=image/crop,x_0,y_0,w_200,h_200,g_se',//小图：以宽度和高度裁剪，裁剪最长为200pc
        'big_resize'   => '?x-oss-process=image/resize,m_lfit,h_1000,w_1000',//大图：以宽度高度缩放，最大值为1000pc
        'mid_resize'   => '?x-oss-process=image/resize,m_lfit,h_750,w_750',//中图：以宽度高度缩放，最大值为500pc
        'small_resize' => '?x-oss-process=image/resize,m_lfit,h_360,w_360',//以宽度高度缩放，最大值为200pc
    );

    public static function active_jump_url($host = '') {//活动跳转地址
        $host = empty($host) ? (config("WAP_HOST") ? config("WAP_HOST") : '') : $host;
        return $active_jump_url = array(
            '1' => 'http://' . $host . '/#/pages/couponcentre/list',//领券中心
            '2' => 'http://' . $host . '/#/pages/cargoant/index',//货柜小蚁首页
            '3' => 'http://' . $host . '/#/pages/profile/order/orderPrice',//订单金额查询页
            '4' => 'http://' . $host . '/#/pages/classes/countrys',//国家馆
            '5' => 'http://' . $host . '/#/pages/goods/seckilllist',//秒杀列表
            '6' => 'http://' . $host . '/#/pages/goods/booklist',//拼团列表
            '7' => 'http://' . $host . '/#/pages/bargaining/index',//砍价列表
            '8' => 'http://' . $host . '/#/pages/distribution/applyJoin',//分销页面
            '9' => 'http://' . $host . '/#/pages/index/index',//首页
        );
    }

    public static function pushType() {//推送消息类型
        return $pushType = array(
            'special'   => '专题',
            'goods'     => '商品',
            'article'   => '活动文章',
            'system'    => '内页项',
            'cut'       => '砍价商品',
            'goods_tag' => '商品标签列表',
            'url'       => '链接地址',
            'luckybag'  => '福袋',
        );
    }

    public static function get_days($time = "", $d = 20) {
        $now = empty($time) ? time() : $time;
        $i   = 1;
        $j   = 0;
        while ($j < $d) {
            $timer = $now + 3600 * 24 * $i;
            $num   = date("N", $timer) - 2; //周一开始
            if ($num >= -1 and $num <= 3) {
                $j++;
                $re = $now + 3600 * 24 * $i;
            }
            $i++;
        }
        return $re;
    }

    /**
     * 组装参数
     */
    public static function getSignContent($data) {
        $buff = '';
        foreach ($data as $k => $v) {
            $buff .= ($k != 'sign' && $v != '' && !is_array($v)) ? $k . '=' . $v . '&' : '';
        }
        return trim($buff, '&');
    }

    /**
     * 对价格进行向上或向下取整
     *  $price    价格
     *  $decimal  保留小数位数
     * $type 1：向上 2：向下
     */
    public static function upDecimal($price, $decimal = 2, $type = 1) {
        //取整入1
        //return  sprintf("%.2f",$price);
        if (empty($decimal)) {
            return ceil($price);
        }
        $price2 = explode('.', $price);
        $price1 = $price2[1] ?? 0;
        $price0 = $price2[0] ?? 0;
        $sublen = strlen($price1);
        if ($sublen < $decimal) {
            $decimal = $sublen;
        }
        //没有小数入1
        if (empty($price1)) {
            return ceil($price);
        }
        $subnum = bcsub($sublen, $decimal);
        //计算小数
        $powsub = bcpow(10, $subnum);
        $price1 = bcdiv($price1, $powsub, $sublen);
        $price1 = ceil($price1);
        $powint = bcpow(10, $decimal);
        $price1 = bcdiv($price1, $powint, $decimal);
        //返回结果
        return bcadd($price0, $price1, $decimal);
    }

    //摩宝验签
    public static function mobao_sign($param = array(), $key = '') {
        $str = '';
        $i   = 1;
        foreach ($param as $k => $v) {
            if ($i == 1) {
                $str .= $k . '=' . $v;
            } else {
                $str .= '&' . $k . '=' . $v;
            }
            $i++;
        }
        $str .= $key;//print_r($str);
        return md5($str);
    }

    public static function msectime() {
        list($msec, $sec) = explode(' ', microtime());
        $msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
        return $msectime;
    }

    /**
     * 删除指定标签
     *
     * @param array $tags 删除的标签  数组形式
     * @param string $str html字符串
     * @param bool $content true保留标签的内容text
     * @return mixed
     */
    public static function stripHtmlTags($tags, $str, $content = true) {
        $html = [];
        // 是否保留标签内的text字符
        if ($content) {
            foreach ($tags as $tag) {
                $html[] = '/<' . $tag . '.*?>[\s|\S]*?<\/' . $tag . '>/';
                $html[] = '/<' . $tag . '.*?>/';
            }
        } else {
            foreach ($tags as $tag) {
                $html[] = "/(<(?:\/" . $tag . "|" . $tag . ")[^>]*>)/is";
            }
        }
        $data = preg_replace($html, '', $str);
        return $data;
    }

    //输出<div><p>这里是p标签</p><img src="" alt="这里是img标签"><br></div>;

    public static function create_invite_code($num) {
        $code = 'ABCDEFGHJKMNPQRSTUVWXYZ';
        $rand = $code[rand(0, 22)]
            . strtoupper(dechex(date('m')))
            . date('d')
            . substr(time(), -5)
            . substr(microtime(), 2, 5)
            . sprintf('%02d', rand(0, 99));
        for (
            $a = md5($rand, true),
            $s = '23456789ABCDEFGHJKMNPQRSTUVWXYZ',
            $d = '',
            $f = 0;
            $f < $num;
            $g = ord($a[$f]),
            $d .= $s[($g ^ ord($a[$f + 8])) - $g & 0x1e],
            $f++
        ) ;
        return $d;
    }

    public static function kq_ck_null($kq_va, $kq_na) {
        if ($kq_va == "") {
            return $kq_va = "";
        } else {
            return $kq_va = $kq_na . '=' . $kq_va . '&';
        }
    }


    public static function removeEmoji($text) {
        $len     = mb_strlen($text);
        $newText = '';

        for ($i = 0; $i < $len; $i++) {
            $str = mb_substr($text, $i, 1, 'utf-8');
            if (strlen($str) >= 4) {
                $newText .= '*';
            } else {
                $newText .= $str;
            }

        }
        return $newText;
    }

    /**
     * 获取分组功能
     * @param array $data
     */
    public static function getArrayGroupData(array $data, string $_fileds) {
        if (empty($data)) {
            return [];
        }

        $temp = [];
        foreach ($data as $item) {
            $temp[$item[$_fileds]][] = $item;
            unset($item);
        }

        return $temp;
    }

    /**
     * 根据二维数组某个字段的值查找数组,只适用于不重复的指定键值
     * @param $array
     * @param $index 键名
     * @param $value 值
     * @return array
     */
    public static function filterByValue(array $array, string $index, string $value) {
        $newarray = [];
        if (is_array($array) && count($array) > 0) {
            foreach (array_keys($array) as $key) {
                $temp[$key] = $array[$key][$index];
                if ($temp[$key] == $value) {
                    $newarray = $array[$key];
                }
            }
        }
        return $newarray;
    }
}